/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

/* Device header file */
#if defined(__dsPIC33E__)
	#include <p33Exxxx.h>
#elif defined(__dsPIC33F__)
	#include <p33Fxxxx.h>
#endif

#include "user.h"            /* variables/params used by user.c               */
#include <stdbool.h>         /* For true/false definition                     */
#include <adc.h>
#include "xlcd.h"
#include <uart.h>
#include <pps.h>    /*Header for Pin Re-Mapping peripheral library functions*/
#include "leds.h"
/******************************************************************************/
/* User Functions                                                             */
/******************************************************************************/

/* <Initialize variables in user.h and insert code for user algorithms.> */

void InitApp(void)
{
    /*Valores de UART 1*/
    /* Holds the value of baud register  */
    unsigned int baudvalue;
    /* Holds the value of uart config reg */
    unsigned int U1MODEvalue;
    /* Holds the information regarding uart
    TX & RX interrupt modes */
    unsigned int U1STAvalue;

    /* Initialize User Ports/Peripherals/Project here */

    /*Configuración de la dirección de los puertos y si son analógicos o
     * digitales */
    /*Los puestos libres son configruados como salidas y puestos a 0 para
     * disminuir la generación de ruido en el circuito*/

    TRISBbits.TRISB0 = 0;
    TRISBbits.TRISB1 = 0;
    TRISBbits.TRISB2 = 0;
    TRISBbits.TRISB3 = 0;
    TRISBbits.TRISB4 = 0;
    TRISBbits.TRISB5 = 0;
    TRISBbits.TRISB6 = 0;
    TRISBbits.TRISB7 = 0;
/*AN8 debe ser entrada analógica pra el conversor ADC*/
    TRISBbits.TRISB9 = 0;
    TRISBbits.TRISB10 = 0;
    TRISBbits.TRISB11 = 0;
    TRISBbits.TRISB12 = 0;
    TRISBbits.TRISB13 = 0;
    TRISBbits.TRISB14 = 0;
    TRISBbits.TRISB15 = 0;

    LATBbits.LATB0 = 0;
    LATBbits.LATB1 = 0;
    LATBbits.LATB3 = 0;
    LATBbits.LATB4 = 0;
    LATBbits.LATB5 = 0;
    LATBbits.LATB6 = 0;
    LATBbits.LATB7 = 0;

    LATBbits.LATB9 = 0;
    LATBbits.LATB10 = 0;
    LATBbits.LATB11 = 0;
    LATBbits.LATB12 = 0;
    LATBbits.LATB13 = 0;
    LATBbits.LATB14 = 0;
    LATBbits.LATB15 = 0;

    TRISDbits.TRISD0 = 0;
    TRISDbits.TRISD1 = 1; /*Este pin está conectado al interruptor S2*/
    TRISDbits.TRISD2 = 1; /*Este pin está conectado al interruptor S3*/
    TRISDbits.TRISD3 = 0; /*Led 1*/
    TRISDbits.TRISD6 = 0; /*Led 2*/
    TRISDbits.TRISD7 = 0; /*Led 3*/
    TRISDbits.TRISD8 = 0;
    TRISDbits.TRISD9 = 0;
    TRISDbits.TRISD10 = 0;
    TRISDbits.TRISD11 = 0;

    LATDbits.LATD0 = 0;
    LATDbits.LATD8 = 0;
    LATDbits.LATD9 = 0;
    LATDbits.LATD10 = 0;
    LATDbits.LATD11 = 0;

    TRISEbits.TRISE0 = 0;
    TRISEbits.TRISE1 = 0;
    TRISEbits.TRISE2 = 0;
    TRISEbits.TRISE3 = 0;

    LATEbits.LATE0 = 0;
    LATEbits.LATE1 = 0;
    LATEbits.LATE2 = 0;
    LATEbits.LATE3 = 0;

    TRISFbits.TRISF0 = 0;
    TRISFbits.TRISF1 = 0;
    TRISFbits.TRISF3 = 0;
    TRISFbits.TRISF4 = 0;

    LATFbits.LATF0 = 0;
    LATFbits.LATF1 = 0;
    LATFbits.LATF3 = 0;
    LATFbits.LATF4 = 0;

    TRISGbits.TRISG7 = 1; /*Este pin está conectado al interruptor S1*/

    TRISGbits.TRISG8 = 0;
    LATGbits.LATG8 = 0;
    
    TRISGbits.TRISG9 = 1; /*U1RX*/
        
/* Inicialización de periféricos*/

    /*--Inicialización de LEDs ---*/
    LED1 = 0;        /*Todos los leds apagados*/
    LED2 = 0;
    LED3 = 0;

    /*--Inicialización de ADC ---*/

   unsigned int config1, config2, config3, config4;
   unsigned int configport_a, configport_b, configport_c, configport_d;
   unsigned int configport_e, configport_f, configport_g, configport_h;
   unsigned int configport_i, configport_j, configport_k, configscan_h;
   unsigned int configscan_l;

   config1 = ADC_MODULE_ON &
           ADC_IDLE_CONTINUE &
           ADC_AD12B_12BIT  &
           ADC_FORMAT_SIGN_FRACT &
           ADC_SSRC_MANUAL &
           ADC_AUTO_SAMPLING_ON &
           ADC_SAMP_ON;

   config2 = ADC_VREF_AVDD_AVSS &
           ADC_SCAN_OFF &
           ADC_SAMPLES_PER_INT_1 &
           ADC_ALT_BUF_OFF &
           ADC_ALT_INPUT_OFF;

           /* TAD_12bit_min = n * TCY = n / FCY =>
            * n = TAD_12bit_min * FCY
            * 117,6 ns * FCY => 1
            */
   config3 =  ADC_CONV_CLK_SYSTEM &
              ADC_CONV_CLK_1Tcy;

   config4 = ADC_DMA_DIS;

   configport_b = ENABLE_AN8_ANA;
   
   configport_a = configport_c = configport_d = configport_e = configport_f = 
           configport_g = configport_h = configport_i = configport_j = 
           configport_k = configscan_h = configscan_l = 0;

   OpenADC1(config1, config2, config3, config4, configport_a, configport_b,
                    configport_c, configport_d, configport_e, configport_f,
                    configport_g, configport_h, configport_i, configport_j,
                    configport_k, configscan_h,  configscan_l);
   
   SetChanADC1(0, ADC_CH0_POS_SAMPLEA_AN8 & ADC_CH0_NEG_SAMPLEA_VREFN);

  /*--Inicialización del LCD ---*/
  /*Modo de transferencia de 4 bits y dos filas de caracteres*/

   OpenXLCD(FOUR_BIT & TWO_LINE);


   /* Inicialización del UART 1*/

   /*Asegurarse de que el módulo UART1 esté apagado*/
    CloseUART1();
    
    /*Mapeo de pines*/
    IN_FN_PPS_U1RX = IN_PIN_PPS_RPI121;     /* UART1 Recibe en pin RPI121 */
    OUT_PIN_PPS_RP120 = OUT_FN_PPS_U1TX  ;  /* UART1 Transmite por pin RP120*/

   /* Configuración de la interrupción por recepción y trasmisión del UART1 */
    ConfigIntUART1(UART_RX_INT_DIS & UART_TX_INT_DIS);
    
    /* Configua el módulo UART1 para transmitir 8 bits de datos
     * con un bit de parada.*/
    baudvalue = BRGVAL;

    U1MODEvalue = UART_EN & UART_IDLE_CON &
                  UART_IrDA_DISABLE & UART_UEN_00 &
                  UART_DIS_WAKE & UART_DIS_LOOPBACK  &
                  UART_DIS_ABAUD & UART_UXRX_IDLE_ONE &
                  UART_BRGH_SIXTEEN & UART_NO_PAR_8BIT &
                  UART_1STOPBIT;

    U1STAvalue  = UART_IrDA_POL_INV_ZERO &
                  UART_SYNC_BREAK_DISABLED &
                  UART_TX_ENABLE  &
                  UART_RX_OVERRUN_CLEAR;

    OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);
    
}